package slent.iespana.es.Quimica.TablaPeriodica.VentanaTablaPeriodica;

import es.iespana.slent.tablaperiodica.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.Rectangle2D;

/**
 * Created by IntelliJ IDEA.
 * User: slent
 * Date: 7/01/2010
 * Time: 11:10:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class Elemento extends JPanel implements MouseListener {
    private boolean isOver;
    private boolean marcado;
    private Dimension tamanio;
    private String simbolo;
    private String nombre;
    private String numeroAtomico;

    private TablaPeriodica.Elemento elementoRelacionado;

    private JLabel labelSimbolo;
    private JLabel labelNombre;
    private JLabel labelNumeroAtomico;

    public Elemento() {
        this("He", "Helio", "2");
    }

    public Elemento(TablaPeriodica.Elemento elementoRelacionado) {
        this.setSimbolo(elementoRelacionado.getSimbolo());
        this.setNombre(elementoRelacionado.getNombre());
        this.setNumeroAtomico(elementoRelacionado.getNumeroAtomico().toString());
        this.elementoRelacionado=elementoRelacionado;

        setOpaque(false);
        tamanio = new Dimension(50, 50);
        setMinimumSize(tamanio);
        setMaximumSize(tamanio);
        setSize(tamanio);
        setPreferredSize(tamanio);
        addMouseListener(this);
        crearGUI();
    }

    public Elemento(String simbolo, String nombre, String numeroAtomico) {
        this.setSimbolo(simbolo);
        this.setNombre(nombre);
        this.setNumeroAtomico(numeroAtomico);

        setOpaque(false);
        tamanio = new Dimension(50, 50);
        setMinimumSize(tamanio);
        setMaximumSize(tamanio);
        setSize(tamanio);
        setPreferredSize(tamanio);
        addMouseListener(this);
        crearGUI();
    }

    private void crearGUI() {
        labelSimbolo = new JLabel(simbolo, SwingConstants.CENTER);
        labelSimbolo.setFont(new Font("SansSerif", Font.BOLD, 20));
        labelNombre = new JLabel(nombre, SwingConstants.CENTER);
        labelNombre.setFont(new Font("Serif", Font.ITALIC, 11));
        labelNumeroAtomico = new JLabel(numeroAtomico, SwingConstants.RIGHT);
        labelNumeroAtomico.setFont(new Font("SansSerif", Font.PLAIN, 10));

        setLayout(new BorderLayout());
        add(labelSimbolo, BorderLayout.CENTER);
        add(labelNombre, BorderLayout.SOUTH);
        add(labelNumeroAtomico, BorderLayout.NORTH);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));


        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);
        g2d.setStroke(new BasicStroke(5));
        RoundRectangle2D.Double rect = new RoundRectangle2D.Double(2, 2, tamanio.getWidth() - 5, tamanio.getHeight() - 5, 15, 15);
        g2d.setColor(Color.BLACK);
        g2d.draw(rect);
        if (isOver || marcado) {
            GradientPaint gp = new GradientPaint(0, 0, Color.white, (float) tamanio.getWidth(), (float) tamanio.getHeight(), Color.BLUE, false);
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.8f));
            g2d.setPaint(gp);
            g2d.fill(rect);
        } else {
            GradientPaint gp = new GradientPaint(0, 0, Color.white, (float) tamanio.getWidth(), (float) tamanio.getHeight(), Color.GRAY, false);
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.8f));
            g2d.setPaint(gp);
            g2d.fill(rect);
        }

        super.paintComponent(g);
    }

    public void mouseClicked(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mousePressed(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseReleased(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseEntered(MouseEvent e) {
        this.isOver = true;
        repaint();
    }

    public void mouseExited(MouseEvent e) {
        this.isOver = false;
        repaint();
    }

    public String getSimbolo() {
        return simbolo;
    }

    public void setSimbolo(String simbolo) {
        this.simbolo = simbolo;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getNumeroAtomico() {
        return numeroAtomico;
    }

    public void setNumeroAtomico(String numeroAtomico) {
        this.numeroAtomico = numeroAtomico;
    }

    public TablaPeriodica.Elemento getElementoRelacionado() {
        return elementoRelacionado;
    }

    public void setElementoRelacionado(TablaPeriodica.Elemento elementoRelacionado) {
        this.elementoRelacionado = elementoRelacionado;
    }

    /**
     *Se encarga de seleccionar el elemento.
     * Lo pone de color para habilitar el marcado
     */
    public void seleccionar(){
        marcado=true;
        repaint();
    }

    public void desmarcar(){
        marcado=false;
        repaint();
    }
}
